﻿<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Bezdelnick
 * Date: 12.04.13
 * Time: 15:15
 * To change this template use File | Settings | File Templates.
 */
require_once("Config.php");
require_once("common/Lang.php");
require_once("persistence/News.php");
require_once("dao/NewsDao.php");

$newsDao = new NewsDao();
$news = $newsDao->load();

if (isset($_POST['action'])) {
    if ($_POST['action'] == "add") {
        if (isset($_POST['add_news_name'])) {
            $createdNews = new News();
            $createdNews->setName($_POST['add_news_name']);
            $createdNews->setDescription($_POST['add_news_description']);
            $createdNews->setPublicDate($_POST['add_news_public_date']);
            $newsDao->insert($createdNews);
        }
    } else if ($_POST['action'] == "edit") {
        if (isset($_POST['edit_news_id']) && isset($_POST['edit_news_name'])) {
            $editedNews = new News();
            $editedNews->setId($_POST['edit_news_id']);
            $editedNews->setName($_POST['edit_news_name']);
            $editedNews->setDescription($_POST['edit_news_description']);
            $editedNews->setPublicDate($_POST['edit_news_public_date']);
            $newsDao->update($editedNews);
        }
    } else if ($_POST['action'] == "remove") {
        if (isset($_POST['remove_news_id'])) {
            $newsDao->delete($_POST['remove_news_id']);
        }
    }
}
?>

<!-- Content table -->
<table class="content-table" border="0">
    <thead style="background-color: #ffd700;">
    <tr>
        <td style="width: 10%;">ID</td>
        <td style="width: 25%;">Name</td>
        <td style="width: 30%;">Description</td>
        <td style="width: 10%;">Publication date</td>
        <td style="width: 25%;">Operations</td>
    </tr>
    </thead>
    <tbody>
    <? foreach ($news as $nw) { ?>
    <tr style="background-color: #efefef;">
        <td><?= $nw->getId() ?></td>
        <td><?= $nw->getName() ?></td>
        <td><?= $nw->getDescription() ?></td>
        <td><?= $nw->getPublicDate() ?></td>
        <td style="text-align: center;">
            <div class="jquery-button edit-news-link"
                 itemId="<?= $nw->getId() ?>"><?= Lang::EDIT ?>
            </div>
            <div class="jquery-button remove-news-link"
                 itemId="<?= $nw->getId() ?>"><?= Lang::REMOVE ?>
            </div>
        </td>
    </tr>
        <? } ?>
    </tbody>
</table>